package com.egao.common.module.student.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.module.student.constants.StudentSignConstants;
import com.egao.common.module.student.constants.StudentSignDispatchConstants;
import com.egao.common.module.student.entity.StudentSign;
import com.egao.common.module.student.entity.StudentSignDispatch;
import com.egao.common.module.student.mapper.StudentSignDispatchMapper;
import com.egao.common.module.student.service.StudentSignDispatchService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 学生毕业去向派遣信息接口实现类
 * @author lym
 */
@Service
public class StudentSignDispatchServiceImpl extends ServiceImpl<StudentSignDispatchMapper, StudentSignDispatch> implements StudentSignDispatchService {

    /**
     * 更新所有的信息状态为无效
     * 返回上一个毕业去向派遣信息 没有等于null
     * @param studentSignId 学生毕业去向ID
     * @return StudentSignDispatch OR null
     */
    public StudentSignDispatch updateState(Integer studentSignId) {
        List<StudentSignDispatch> list = baseMapper.selectList(new QueryWrapper<StudentSignDispatch>()
                .eq("student_sign_id", studentSignId)
                .orderByDesc("id")
        );

        if (list == null || list.size() == 0) {
            return null;
        }

        StudentSignDispatch studentSignDispatch = new StudentSignDispatch();
        studentSignDispatch.setState(StudentSignDispatchConstants.STATE_INVALID);
        baseMapper.update(studentSignDispatch, new UpdateWrapper<StudentSignDispatch>().eq("student_sign_id", studentSignId));

        return list.get(0);
    }

    @Override
    public void saveByStudentSign(StudentSign studentSign) {
        StudentSignDispatch update = updateState(studentSign.getId());

        StudentSignDispatch studentSignDispatch = new StudentSignDispatch();

        BeanUtil.copyProperties(studentSign, studentSignDispatch);
        studentSignDispatch.setStudentSignId(studentSign.getId());
        studentSignDispatch.setStudentId(studentSign.getStudentId());
        studentSignDispatch.setState(StudentSignDispatchConstants.STATE_VALID);
        studentSignDispatch.setLastId(update != null ? update.getId() : 0);

        switch (studentSignDispatch.getDispatchHiredWay()) {
            case StudentSignConstants.LYFS_03:
            case StudentSignConstants.LYFS_14:
            case StudentSignConstants.LYFS_22:
                break;
            default:
                studentSignDispatch.setAbroadTalentImg("");
                break;
        }

        baseMapper.insert(studentSignDispatch);
    }

    @Override
    public StudentSign rollBack(StudentSign studentSign) {
        int id = studentSign.getId();
        String state = studentSign.getState();


        StudentSignDispatch update = updateState(studentSign.getId());

        if (update != null && update.getLastId() != null && update.getLastId() != 0) {
            StudentSignDispatch studentSignDispatch = baseMapper.selectById(update.getLastId());
            studentSignDispatch.setState(StudentSignDispatchConstants.STATE_VALID);
            baseMapper.updateById(studentSignDispatch);

            // 将申请改派前的派遣信息覆盖到毕业去向信息中
            BeanUtil.copyProperties(studentSignDispatch, studentSign);
            // 重新覆盖一下原始state记录 copyProperties两个类中都有state但意义不一样
            studentSign.setId(id);
            studentSign.setState(state);
        }

        return studentSign;

    }
}
